<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<addForm v-show="addStatus" ref="addFormRef" />
		<view v-show="!addStatus">
			<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="200"
				label-align="left">
				<u-form-item label="申请单单号" v-if="judgeShow('tg003')">
					<u-input v-model="dataForm.tg003" disabled></u-input>
				</u-form-item>
				<u-form-item label="申请人" v-if="judgeShow('tg002')">
					<jnpf-user-select v-model="dataForm.tg002" @input="changeName"
						:disabled="judgeWrite('tg002')|| setting.readonly" />
				</u-form-item>
				<u-form-item label="部门" v-if="judgeShow('tg011')">
					{{dataForm.tg013}}
				</u-form-item>
				<u-form-item label="发生日期" v-if="judgeShow('tg007')">
					<lys-date v-if="showte005" type="1" @cancleTime="showte005 = false" @submitTime="submitte005"
						:disabled="judgeWrite('tg007')"></lys-date>
					<view style="display: flex;justify-content: space-between;width: 100%;">
						<view>
							{{dataForm.tg007}}
						</view>
						<view>
							<u-icon name="arrow-down-fill" color='#ccc' @click="showte005=true"
								:disabled="judgeWrite('tg007')|| setting.readonly"></u-icon>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="申请金额合计" v-if="judgeShow('tg012')">
					<u-input v-model="dataForm.tg012" type="number" disabled> </u-input>
				</u-form-item>
				<u-form-item label="备注" v-if="judgeShow('tg009')">
					<u-input v-model="dataForm.tg009" :disabled="judgeWrite('tg009')|| setting.readonly"></u-input>
				</u-form-item>
				<u-form-item label="附件" type="textarea" v-if="judgeShow('tg010')">
					<jnpf-file v-model="dataForm.tg010" :disabled="judgeWrite('tg010')|| setting.readonly" />
				</u-form-item>
				<view class="jnpf-table" style="margin-top: 24rpx">
					<view class="jnpf-table-item" style="margin-bottom: 24rpx" v-for="(item, i) in dataForm.oastkList"
						:key="i">
						<u-form-item label="序号" style="background-color: aqua">
							{{ i + 1 }}
						</u-form-item>
						<u-form-item label="费用类型" v-if="judgeShow('oastkList-tk003')">
							<view style="display: flex;justify-content: space-between;width: 100%;">
								<view>
									{{item.tk003Name}}
								</view>
								<view>
									<u-icon name="arrow-down-fill" color='#ccc' @click="chioceOastdBox(i)" :disabled="
                      judgeWrite('oastkList') || judgeWrite('oastkList-tk003')|| setting.readonly
                    "></u-icon>
								</view>
							</view>
						</u-form-item>
						<u-form-item label="申请金额" v-if="judgeShow('oastkList-tk004')">
							<u-input v-model="item.tk004" type="number" :disabled="
                      judgeWrite('oastkList') || judgeWrite('oastkList-tk004')|| setting.readonly
                    "></u-input>
						</u-form-item>
						<u-form-item label="备注" v-if="judgeShow('oastkList-tk005')">
							<u-input v-model="item.tk005" :disabled="
                      judgeWrite('oastkList') || judgeWrite('oastkList-tk005')|| setting.readonly
                    "></u-input>
						</u-form-item>
					</view>
				</view>
			</u-form>
		</view>
		<u-popup v-model="popupshow" mode="bottom" height="50vh">
			<view class="content">
				<scroll-view scroll-y="true" style="height: 800rpx;">
					<zsf-table :radio='true' @getRow='getSelectData' ref="product" :title='productionQueueTitle'
						:data="tableList"></zsf-table>
				</scroll-view>
				<view class="confrim-btn">
					<u-button @click="close">确定</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import addForm from "./addForm.vue";
	import comMixin from "../mixin";
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from "@/api/apply/order";
	import {
		getOastg,
		BomcbListInit,
		putOastg,
		getOastdList
	} from "@/api/workFlow/flowBefore";
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,
		getUserAll
	} from "@/api/common";
	export default {
		mixins: [comMixin],
		components: {
			addForm
		},
		computed: {
			getTb019Computed() {
				return (id) => {
					const options = this.options.filter((item) => id.includes(item.id));
					return options.length > 0 ? options[0].fullName : "";
				};
			},
		},
		watch: {
			'dataForm.oastkList': {
				handler(newVal) {
					let total = 0;
					newVal.forEach(item => {
						if (item.tk004) {
							total += parseFloat(item.tk004);
						}
					});
					this.dataForm.tg012 = total;
				},
				deep: true // 深度监听数组内部的变化
			}
		},
		data() {
			return {
				productionQueueTitle: [{
						title: "序号",
						key: "sid",
						width: '80',
					},
					{
						title: "费用类型编号",
						key: "td001",
						width: '80',
					},
					{
						title: "费用类型名称",
						key: "td002",
						width: '80',
					},

				],
				popupshow: false,
				showte005: false,
				addStatus: '',
				opType: 0,
				show: false,
				dataForm: {},
				id: "",
				tk003: '',
				tk003Name: '',
				index: null,
				showForm: [],
				formOperatesList: [],
				tb019Options: [],
				options: [{
						fullName: "必选，唯一",
						id: "1",
					},
					{
						fullName: "必选，至少一个",
						id: "2",
					},
					{
						fullName: "可选，唯一",
						id: "3",
					},
					{
						fullName: "任意选",
						id: "4",
					},
				],
			};
		},
		methods: {
			close() {
				this.popupshow = false;
				this.dataForm.oastkList[this.index].tk003Name = this.tk003Name
				this.dataForm.oastkList[this.index].tk003 = this.tk003
			},
			getSelectData(e) {
				this.tk003 = e.data.td001;
				this.tk003Name = e.data.td002;
			},
			chioceOastdBox(index) {
				if (!this.dataForm.tg011) {
					this.$refs.uToast.show({
						title: '部门不能为空！',
						type: 'error',
					})
					return
				}

				this.index = index; //当前下标
				//报销部门
				var queryParam = {
					td006: this.dataForm.tg011,
					currentPage: 1,
					pageSize: 500,
					sort: 'asc',
					sidx: 'TD001',
				};
				getOastdList(queryParam).then(res => {
					this.popupshow = true
					this.tableList = res.data.list.map((item, index) => {
						return {
							sid: index + 1,
							...item
						}
					})

				})
			},
			submitte005(time, type, timeInt) {
				let tg007 = time.split("-");
				this.dataForm.tg007 = tg007[0] + tg007[1] + tg007[2]
				this.showte005 = false;
			},
			changeName(id) {
				getUserAll().then(res => {
					let item = res.data.filter(o => o.id === id)[0]
					this.dataForm.tg011 = item.departmentId; //部门编号
					this.dataForm.tg013 = item.department; //部门名称
				})
			},
			init(data, id, flowFormContentJson, addStatus) {
				this.addStatus = addStatus
				this.opType = data.opType;
				this.setting = data;
				this.formOperatesList = data.formOperates;
				this.id = id;
				if (!this.addStatus) {
					this.getInfoFn(id);
				} else {
					console.log('新增');
					this.$refs.addFormRef.getInfoFn(data)

				}
			},
			getInfoFn(id) {
				getOastg(id).then((res) => {
					if (res.code == 200) {
						this.dataForm = res.data;
						// this.getDataInterfaceDataInfoME001(this.dataForm.tg002);
						// this.getDataInterfaceDataInfoMV001(this.dataForm.tg011);
					}
				});
			},
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return true;
				let arr = this.setting.formOperates.filter(o => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return item.read;
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return false;
				let arr = this.setting.formOperates.filter(o => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return !item.write;
			},
			queryChildBachelor(tb004) {
				let query = {
					tb004,
				};
				BomcbListInit(query).then((res) => {
					this.showForm = res.data;
					this.show = true;
				});
			},
			//  申请人
			getDataInterfaceDataInfoME001(id) {
				let data = {
					id,
					interfaceId: "445425382452126213",
					propsValue: "MV001",
					relationField: "JoinStr",
				};
				getDataInterfaceDataInfo("445425382452126213", data).then((res) => {
					this.dataForm.tg002 = res.data.JoinStr;
				});
			},
			// 部门
			getDataInterfaceDataInfoMV001(id) {
				let data = {
					id,
					interfaceId: "362111557984852549",
					propsValue: "ME001",
					relationField: "JoinStr",
				};
				getDataInterfaceDataInfo("362111557984852549", data).then((res) => {
					this.dataForm.tg011 = res.data.JoinStr;
				});
			},
			selfGetInfo() {
				Info(this.setting.id).then((res) => {
					this.dataForm = res.data;
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson);
					}
				});
			},
			submit(eventType, config) {
				this.dataForm.flowId = config.flowId
				this.dataForm.flowState = eventType === "submit" ? 0 : 1;
				if (eventType === "audit" || eventType === "reject") {
					this.$emit("eventReceiver", this.dataForm, eventType);
					return;
				}
				let name = eventType === "submit" ? '提交' : '保存';
				uni.showModal({
					title: "提示",
					content: `您确定要${name}当前流程吗？`,
					success: (res) => {
						if (res.confirm) {
							this.selfHandleRequest();
						}
					},
				});
			},
			selfHandleRequest() {
				putOastg(this.dataForm.tg001, this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});
				});
			},
			initData() {
				getGoodsList().then((res) => {
					this.goodsOptions = res.data.list;
				});
				getCustomerList().then((res) => {
					this.customerOptions = res.data.list;
				});
			},
			selfInit() {
				this.addGoods();
				this.addPlan();
			},
			onChange(e) {
				const user = e[0];
				this.dataForm.salesmanName = user.fullName;
			},
			addGoods() {
				const item = {
					goodsId: "",
					goodsCode: "",
					goodsName: "",
					specifications: "",
					unit: "",
					qty: "",
					price: "",
					amount: "",
					discount: null,
					cess: null,
					actualPrice: "",
					actualAmount: "",
					description: "",
				};
				this.dataForm.goodsList.push(item);
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1);
			},
			addPlan() {
				const item = {
					receivableDate: "",
					receivableRate: "",
					receivableMoney: "",
					receivableMode: "",
					abstract: "",
				};
				this.dataForm.collectionPlanList.push(item);
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1);
			},
			onGoodsChange(e, i) {
				const goods = e[0];
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: "",
				};
				this.$forceUpdate();
			},
			onCustomerChange(e) {
				const customer = e[0];
				this.dataForm.customerName = customer.text;
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(
					parseFloat(row.price) * parseFloat(row.qty)
				);
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(
					discountPrice * (1 + row.cess / 100)
				);
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(
					parseFloat(row.actualPrice) * parseFloat(row.qty)
				);
				this.$forceUpdate();
			},
		},
	};
</script>